﻿using System;
using System.Diagnostics;


namespace WindowsFormsApplication1
{
    public class SendParser
    {
        //The current IRC GUI form
        protected IRCForm form;
        protected IRC client;

        public SendParser(IRCForm form, IRC client)
        {
            this.form = form;
            this.client = client;
        }

        public void parser(String command, String channelName)
        {
            if (command.StartsWith("/"))
            {
                //Remove the slash and make everything lowercase for easy parsing.
                command = command.Replace("/", "");
                command = command.ToLower();

                //Part channel
                if (command.StartsWith("part"))
                {
                    partChannel(command);
                }

                //Change nickname.
                if (command.StartsWith("nick"))
                {
                    changeNickname(command);
                }

                //Join a channel.
                if (command.StartsWith("join"))
                {
                    joinChannel(command);
                }

                //Display command functions.
                if (command.StartsWith("commands"))
                {
                    commandList();
                }

                //Quit the server
                if (command.StartsWith("quit"))
                {
                    quitServer();
                }

                if (command.StartsWith("server"))
                {
                    connectToServer(command);
                }

                if (command.StartsWith("msg"))
                {
                    privateMessage(command.Replace("msg ", ""));
                }
            }
            else
            {
                string[] parameters = null;
                char[] splitchar = {' '};
                parameters = command.Split(splitchar);

                try
                {
                    //Form the PRIVMSG string
                    string privmsg = "PRIVMSG " + channelName + " :";
                    foreach (string s in parameters)
                    {
                        privmsg += s + " ";
                    }
                    client.sendMessage(privmsg);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                    form.writeIncomingData("Something went wrong");
                }
            }
            form.writeIncomingData(command);
        }   

        public void partChannel(string command)
        {
            string[] parameters = null;
            char[] splitchar = { ' ' };
            parameters = command.Split(splitchar);

            try
            {
                client.sendMessage("PART " + parameters[1]);
                form.writeIncomingData("You have left channel " + parameters[1]);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                form.writeIncomingData("To part a channel use: /part <#channel_name>");
            }
        }

        public void privateMessage(string command)
        {
            string[] parameters = null;
            char[] splitchar = { ' ' };
            parameters = command.Split(splitchar);

            try
            {
                //Form the PRIVMSG string
                string privmsg = "PRIVMSG ";
                int count = 1;
                foreach (string s in parameters)
                {
                    if(count == 1)
                        privmsg += s + " :";
                    else
                        privmsg += s + " ";
                    count++;
                }
                client.sendMessage(privmsg);
               // Debug.WriteLine("PRIVMSG " + parameters[1] + parameters[2]);
                form.writeIncomingData(parameters[2]);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);

            }
        }

        public void changeNickname(string command)
        {
            string[] parameters;
            char[] splitchar = { ' ' };
            parameters = command.Split(splitchar);
            try
            {
                client.sendMessage("NICK " + parameters[1]);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                form.writeIncomingData("To change your nickname use: /nick <nickname>");
            }
        }

        public void joinChannel(string command)
        {
            string[] parameters;
            char[] splitchar = { ' ' };
            parameters = command.Split(splitchar);
            try
            {
                client.sendMessage("JOIN " + parameters[1]);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                form.writeIncomingData("To join a channel use: /join <#channel_name>");
            }
        }

        public void quitServer()
        {
            client.sendMessage("QUIT");
        }

        public void commandList()
        {
            form.writeIncomingData(Environment.NewLine + "/server <server name> <port>    -Connect to a server"
                                      + Environment.NewLine + "/commands   -See a list of available commands"
                                      + Environment.NewLine + "/nick <name>   -Change your nickname"
                                      + Environment.NewLine + "/join <#channel>   -Join a channel"
                                      + Environment.NewLine + "/msg <nick> <message>  -Message a user on the server/channel"
                                      + Environment.NewLine + "/part <#channel>   -Leave a channel"
                                      + Environment.NewLine + "/quit   -Quit the RuIRC client");
        }

        public void connectToServer(String command)
        {
            string[] parameters;
            char[] splitchar = { ' ' };
            parameters = command.Split(splitchar);
            try
            {
                form.connectClient(parameters[1], Int32.Parse(parameters[2]));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                form.writeIncomingData("To connect to a server use: /server <server name> <port>");
            }
        }
      
    }
}    



